package com.huluobo.a2109mouthdemo.contact;

import android.renderscript.Sampler;

import com.huluobo.a2109mouthdemo.base.BasePresenter;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by LC on 2023/12/4.
 */
public class ContactPresenter extends BasePresenter<ContactContract.View> implements ContactContract.Presenter {

    List<ContactListItemBean> contactListItemBeans;

    @Override
    public void loadContact() {
        EMClient.getInstance().contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                contactListItemBeans = new ArrayList<>();
                Collections.sort(value);
                for (int i = 0; i < value.size(); i++) {
                    boolean showFirstLetter = false;
                    if (i == 0 || value.get(i).charAt(0) != value.get(i - 1).charAt(0)) {
                        showFirstLetter = true;
                    }

                    ContactListItemBean contactListItemBean = new ContactListItemBean(
                            value.get(i),
                            Character.toUpperCase(value.get(i).charAt(0)),
                            showFirstLetter
                    );

                    contactListItemBeans.add(contactListItemBean);
                }
                if (isAttach()) {
                    getView().onLoadContactSuccess(contactListItemBeans);
                }
            }

            @Override
            public void onError(int error, String errorMsg) {
                if (isAttach()) {
                    getView().onLoadContactFailed(errorMsg);
                }
            }
        });
    }
}
